import pandas as pd
from jili.data.tool import mark_flag,hold_until_flag
from jili.core.printlog import print
from jili.data.db import getdb_client,insert_one
from jili.core import load,save
import os,datetime
def deal(url=r"G:\data\stock",ip="127.0.0.1",type="timestamp"):
    base_url=os.path.join(url,"hfq","k1d")
    url_byobj=os.path.join(base_url,"byobj")
    url_bymonth = os.path.join(base_url, "bymonth")
    url_byobjyear = os.path.join(base_url, "byobjyear")
    db=getdb_client(ip=ip,dbname="stock")
    info_cu=db["stock_info"]

    if not hold_until_flag("k1d_hfq_data",islocal=False):
        return None
    objs={}
    k1d_cu=db["stock_k1d_hfq_0"]
    now=datetime.datetime.now()
    for i in info_cu.find({"list_status" : "L"}):
        if i["delist_date"]:
            print("delist_date",i["obj"], i["delist_date"])
            continue

        obj=i["obj"]
        url_k=os.path.join(url_byobjyear,now.strftime("%Y"),obj+"_k1d.pkl")
        if os.path.exists(url_k):
            a=load(url_k)
            start = a[-1]["timekey"]
            objs[obj]={"obj":obj,"timekey":{"$gt":start}}
        else:
            objs[obj]={"obj":obj}
        print(obj,objs[obj])
    print("objs",len(objs))
    k1d={}
    for obj,v in objs.items():
        print("select", obj, v)
        t=[]
        for i in k1d_cu.find(v):
            del i["_id"]
            if type=="timestamp":
                i["timekey"]=pd.Timestamp(i["timekey"])
            t.append(i)
        if t:
            k1d[obj]=t
#"""
    #byobj
    k1d_byyear={}
    k1d_bymonth={}
    print("k1d",len(k1d))
    for obj,v in k1d.items():
        print("add",obj,v[0]["timekey"],v[-1]["timekey"],len(v))
        for i in v:
            timekey=i["timekey"]
            year=timekey.strftime("%Y")
            if year not in k1d_byyear.keys():
                k1d_byyear[year]={}
            if obj not in k1d_byyear[year].keys():
                k1d_byyear[year][obj]=[]
            k1d_byyear[year][obj].append(i)
            monthkey=timekey.strftime("%Y%m")
            if monthkey not in k1d_bymonth.keys():
                k1d_bymonth[monthkey]={}
            if timekey not in k1d_bymonth[monthkey].keys():
                k1d_bymonth[monthkey][timekey] = {}
            k1d_bymonth[monthkey][timekey][obj]=i
    for obj,v in k1d.items():
        #print("add",obj,v[0]["timekey"],v[-1]["timekey"],len(v))
        url_k = os.path.join(url_byobj, obj + "_k1d.pkl")
        if os.path.exists(url_k):
            a = load(url_k)
            a.extend(v)
            save(a, url_k)
        else:
            save(v,url_k)
    print("update byobj")
    #byobjyear
    for year,v in k1d_byyear.items():
        for obj,v1 in v.items():
            url_k=os.path.join(url_byobjyear,year, obj + "_k1d.pkl")
            if os.path.exists(url_k):
                a = load(url_k)
                a.extend(v1)
                save(a, url_k)
            else:
                save(v1,url_k)
    print("update byobjyear")
    #bymonth
    for key,v in k1d_bymonth.items():
        url_k = os.path.join(url_bymonth, key + "_k1d.pkl")
        if os.path.exists(url_k):
            a = load(url_k)
            for timekey,v1 in v.items():
                if timekey not in a.keys():
                    a[timekey]={}
                for obj,i in v1.items():
                    a[timekey][obj]=i
            save(a, url_k)
        else:
            save(v, url_k)
    print("update bymonth")
    mark_flag("k1d_data_pkl")
#"""
def check(today,url):
    monthkey = today.strftime("%Y%m")
    #G:\data\stock\hfq\k1d\bymonth
    url_data=os.path.join(url,"hfq","k1d",'bymonth',monthkey+"_k1d.pkl")
    a=load(url_data)
    rst=False
    if today in a.keys():
        if len(a[today])>4300:
            rst=True
    return rst

def send_msg(s):
    key = "c922a88b-b594-4526-b23f-5c7f8065b35f"
    from jili.ibot.chat_qy import chat_qywx
    from jili.tool.pcinfo import getlocal_ip
    chat = chat_qywx(key)
    msg=getlocal_ip()+":"+s
    chat.send_msg(msg)
if __name__ == '__main__':
    from jili.data.db import tradecal as tcl
    import datetime
    d = datetime.datetime.now()
    day = datetime.datetime(d.year, d.month, d.day)
    istday = tcl.istradeday(day)
    if istday:
        print("今天是交易日")
        deal(url=r"G:\data\stock", ip="192.168.10.44")
        rst = check(day, url=r"G:\data\stock")
        if rst:
            s = "今天" + day.strftime("%m-%d") + "日K已正常转存pkl"
        else:
            s = "今天" + day.strftime("%m-%d") + "日K转存pkl:异常"
        send_msg(s)
        print(s)
    print("运行结束")

